<?php

namespace app\proxy\model\setting;

use app\common\model\region\Region as Region1;
use app\common\model\settings\Dic;
use app\proxy\model\proxy\Region;

/**
 * 配送模板模型
 */
class DicModel extends Dic
{
    /**
     * 获取列表
     * @param $param
     * @param $regionIds
     * @param $proxyId
     * @return DicModel[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @Author joey
     * @date 2024/3/28 10:34
     */
    public function getList($param, $regionIds, $proxyId = 0)
    {
        $model = new static();
        $where = $this->disposeWhere($param);
        $model = $this->assWhere($model, $where);
        $lists = $model->where(function ($query) use ($regionIds) {
            $query->whereIn('county_id', $regionIds)->whereOr("province_id", "in", $regionIds)->whereOr("city_id", "in", $regionIds);
        })->order(['sort_num' => 'asc', 'create_time' => 'desc'])
            ->paginate($param);
        if ($proxyId > 0) {
            foreach ($lists as $list) {
                $region_id = $list["province_id"];
                if ($list["city_id"]) {
                    $region_id = $list["city_id"];
                }
                if ($list["county_id"]) {
                    $region_id = $list["county_id"];
                }
                $list["region_name"] = Region1::getMergerName($region_id);
                $list["proxy_name"]  = Region::getProxyName($list, $proxyId);
                $list["can_edit"]    = $list["proxy_id"] == $proxyId ? 1 : 0;
            }
        }
        return $lists;
    }

    /**
     * 新增
     * @param $data
     * @param $proxyId
     * @return bool
     * @Author joey
     * @date 2024/3/28 13:04
     */
    public function add($data, $proxyId)
    {
        $data['app_id']      = self::$app_id;
        $data['proxy_id']    = $proxyId;
        $data['province_id'] = $data["region_id"][0] ?? 0;
        $data['city_id']     = $data["region_id"][1] ?? 0;
        $data['county_id']   = $data["region_id"][2] ?? 0;
        unset($data["region_id"]);
        return $this->save($data);
    }

    /**
     * 修改
     * @param $data
     * @param $proxyId
     * @return bool
     * @Author joey
     * @date 2024/3/28 13:04
     */
    public function edit($data, $proxyId)
    {
        $data['app_id']       = self::$app_id;
        $data["audit_status"] = $data['audit_status'] ?? 0;
        $data['province_id']  = $data["region_id"][0] ?? 0;
        $data['city_id']      = $data["region_id"][1] ?? 0;
        $data['county_id']    = $data["region_id"][2] ?? 0;
        unset($data["region_id"]);
        if ($proxyId != $this["proxy_id"]) {
            $this->error = "你无权修改此配置";
            return false;
        }
        return $this->save($data);
    }

    /**
     * 删除
     * @return bool
     * @Author joey
     * @date 2024/3/28 13:04
     */
    public function remove()
    {
        return $this->where(['id' => $this['id']])->delete();
    }

}
